<?php
defined ( 'IN_TS' ) or die ( 'Access Denied.' );


$all= intval($_GET['all']);
$type= intval($_GET['type']);
$province = $_GET['province'];
$city = $_GET['city'];
$status = $_GET['status'];
$isassist = $_GET['isassist'];
$li = $_GET['li'];
$guan = $_GET['guan'];
// 所有小组
$page = isset ( $_GET ['page'] ) ? intval ( $_GET ['page'] ) : '1';
$lstart = $page * 30 - 30;
$url = tsUrl ( 'group', 'index', array ('all'=>$all,'type'=>$type,'page' => '') );
$order = 'isrecommend desc,';

if($type){
	switch($type){
		case 1:
			if(!$all){$all=1;}
			$order =$order.'count_sto asc,';
			break;
		case 2:
			if(!$all){$all=1;}
			$order =$order.'count_active asc,count_lastduty asc,count_nextduty asc,';
			break;
		case 3:
			if(!$all){$all=3;}
			$order =$order.'count_active,';
			break;
	}
}

$arr = 'isaudit=0';

switch($all){
	case 2:
		$myGroup = array ();
		if ($TS_USER ['userid']) {
			$myGroups = $new ['group']->findAll ( 'group_user', array (
				'userid' => $TS_USER ['userid']
			), null, 'groupid' );
			foreach ( $myGroups as $item ) {
				$myGroup [] = $item ['groupid'];
			}
			$str = implode(',',$myGroup);
			if($str){
				$arr = $arr.' and groupid in('.$str.') ';
			}
		}

		break;
	case 1:
		$arr = $arr.' and isplace=2';
		$order = $order.'status desc,joinway asc,';
		break;
	case 3:
		$arr = $arr.' and isplace=1';
		break;

}
$order = $order.'addtime asc';

if($all==1||$all==3){
	if($type==1){
		$arr=$arr.' and isdonate=1 and count_sto<40';
	}
	if($type==3||$type==2){
		$arr=$arr.' and joinway = 0';
	}
}

if($province){
	$arr = $arr.' and province="'. $province.'" and groupid<>1';
}

if($city){
	$arr = $arr.' and city="'. $city.'" and groupid<>1';
}

if($status){
	$arr = $arr.' and status ='.$status.' and groupid<>1';
	if($status==2){
		$title1 = '运营中';
	}else{
		if($status==1){
			$title1 = '筹备中';
		}else{
			$title1 = '已停运';
		}
	}
}

if($isassist){
	if($isassist==1){
		$arr = $arr.' and isassist = 1 and groupid<>1';
		$title1 = '协办';
	}else{
		$arr = $arr.' and isassist = 0 and groupid<>1';
		$title1 = '主办';
	}
}
if($li){
	if($li==1){
		$arr = $arr.' and iscouncil=1 and groupid<>1';
		$title1 = '成立理事会';
	}else{
		$arr = $arr.' and iscouncil=0 and isdonate=1 and groupid<>1';
		$title1 = '待成立理事会';
	}
}
if($guan){
		$arr = $arr.' and isassist=0 and isserver=0 and status=0 and groupid<>1';
		$title1 = '待成立馆员团队';
}

$strAll =  $new ['group']->findAll ('group',array('isplace'=>2));
foreach($strAll as $v){
	if(!in_array($v['province'],$strPro)){
		$strPro[] = $v['province'];
	}
	if($province==$v['province']){
		if(!in_array($v['city'],$strCity)){
			$strCity[] = $v['city'];
		}
	}

}

$arrGroup = $new ['group']->findAll ( 'group', $arr, $order, null, $lstart . ',30' );

foreach ( $arrGroup as $key => $item ) {
	$arrGroup [$key] ['groupname'] = tsTitle ( $item['groupname'] );
	$strOpen = explode(',',$item['openday']);
	$openday='';
	foreach($strOpen as $v){
		if(!$openday){
			$openday = num2day($v);
		}else{
			$openday = $openday.num2day($v);
		}
	}
	$arrGroup [$key] ['openday'] = $openday;


	if(!$all){
		$arrGroup [$key] ['groupdesc'] = $item ['province'].$item ['city'].$item ['address'];
	}else{
		$arrGroup [$key] ['groupdesc'] = cututf8 ( t(tsDecode($item ['groupdesc'])), 0, 30 );
	}

	if($all==1||$all==3){


		if($all==1){
			$strDuty = aac('duty')->find('duty','groupid='.$item['groupid'].' and status=1 and cateid=1 and addtime>'.strtotime('-7 day'));
			if(!$strDuty&&$arrGroup[$key]['count_active']<6&&$arrGroup[$key]['status']==0&&$arrGroup[$key]['joinway']==0){
				$arrGroup[$key]['sick']=1;
			}else{
				$arrGroup[$key]['sick']=0;
			}
		}

/*
//判断会员是否加入该小组

		if(intval($TS_USER['userid'])){
			$arrGroup[$key]['isGroupUser'] = $new['group']->findCount('group_user',array(
				'userid'=>intval($TS_USER['userid']),
				'groupid'=>$item['groupid']
			));
		}

//判断会员是否月捐人

		if(intval($TS_USER['userid'])){
			$arrGroup[$key]['isStockUser'] = $new['group']->findCount('group_user','userid='.intval($TS_USER['userid']).' and groupid='.$item['groupid'].' and isstock<>0'
			);
		}

//判断待审会员的人数
		if($item['joinway']==2){
			$arrGroup[$key]['isApply']=$new['group']->findCount('group_user_isaudit',array(
				'userid'=>intval($TS_USER['userid']),
				'groupid'=>$item['groupid']
			));
		}*/
	}
}




$groupNum = $new ['group']->findCount ( 'group',$arr);

$pageUrl = pagination ( $groupNum, 30, $page, $url );



/*
// 最新10个小组
$arrNewGroup = $new ['group']->getNewGroup ( '10' );

// 热门话题
$arrTopics = $new ['group']->findAll ( 'group_topic', null, 'count_comment desc', 'groupid,topicid,title,count_comment', 10 );
foreach ( $arrTopics as $key => $item ) {
	$arrTopic [] = $item;
	$arrTopic [$key] ['group'] = $new ['group']->getOneGroup ( $item ['groupid'] );
	$arrTopic [$key] ['title'] = tsTitle ( $item ['title'] );
}
*/
$title = '小组';
if($type==1){
	$title = '馆东报名';
}else{
	if($type==2||$type==3){
		$title = '馆员报名';
	}else{
		if($all==1){
			$title = $province.$city.$title1. '各馆一览';
		}
	}
}

if($strCate){
    $title = $strCate['catename'];
}

$sitekey = $TS_APP['appkey'];
$sitedesc = $TS_APP['appdesc'];
include template ( "index" );